package com.linln.admin.yearaudit;


import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.linln.common.exception.order.OrderException;
import com.linln.common.utils.DateUtil;
import com.linln.common.vo.JsonResult;
import com.linln.modules.order.service.OrderTakecaruserService;
import com.linln.modules.system.domain.Role;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.regex.Pattern;


/**
 * @author 薛超
 * @since 2021-01-12
 * @version 1.0.7
 */
@RequestMapping("/order/takecaruser")
@Controller
public class OrderTakecaruserController {

	@Autowired
	private UserService userService;

	@Autowired
	private OrderTakecaruserService orderTakecaruserService;

	@RequiresPermissions("order:takecaruser:index")
	@RequestMapping("/index")
	public String list() {
		return "/yearaudit/takecaruser/list";
	}




	@RequiresPermissions("order:takecaruser:add")
	@RequestMapping("/add")
	public String add() {
		return "/yearaudit/takecaruser/add";
	}

	@RequiresPermissions("order:takecaruser:edit")
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, Model model) {
		User user = userService.getById(Long.valueOf(id));
		model.addAttribute("user", user);
		return "/yearaudit/takecaruser/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("order:takecaruser:add")
	public JsonResult save(User user) {
		String regex = "^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\\d{8}$";
		if(!Pattern.matches(regex, user.getPhone())){
			return JsonResult.error("手机号码格式错误");
		}
		if(Strings.isBlank(user.getNickname())){
			return JsonResult.error("请填写姓名");
		}
		User oldUser =  userService.getByPhone(user.getPhone());
		if(oldUser!=null && user.getId()==null){
			return JsonResult.error("手机号码已被使用");
		}

		try {
			if(user.getId()!=null){
				if(orderTakecaruserService.updateTakecaruser(user)<1){
					return JsonResult.error("保存失败");
				}
			}else{
				user.setCreateDate(new Date());
				orderTakecaruserService.saveTakecaruser(user);
			}

		} catch (OrderException e) {
			e.printStackTrace();
			return JsonResult.error(e.getMessage());
		}
		return JsonResult.ok();
	}

	/**
	 * 删除
	 */

	@RequiresPermissions("order:takecaruser:remove")
	@RequestMapping("/remove")
	@ResponseBody
	public JsonResult remove(@NotNull(message = "id 不能为空") String id) {

		boolean flag = orderTakecaruserService.isHaveOrder(id);
		if(!flag){
			return JsonResult.error("当前取车人还有带指派订单，请更改订单或稍后再试").setCode(400);
		}
		try {
			orderTakecaruserService.removeTakecaruserById(id);
		}catch (Exception e){
			e.printStackTrace();
			return JsonResult.error(e.getMessage());
		}

		return JsonResult.ok();
	}
}